
clear
do "I:\Workdata\707068\Ida\new\do\FINAL\FINALFINAL\First.do"
********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

drop if timing>3
drop if timing<-3

********************************************************************************

* Number of patients who had the same pre-closure PCP and get the same post closure PCP
cap drop N
bys gp_fe old_new_gp: g N=_N 

* Number of patients in the pre-closure PCP
bys gp_fe: g N_tot=_N

* Share of patients going to the same post closure PCP
g share=N/N_tot
sum share, d

* Define acquired by 85 pct. or more of patients moving to the same post-closure PCP
cap drop NewSample
g NewSample=(share>=0.85)



********************************************************************************
* New and old PCP characteristics to test for selection
********************************************************************************


*** NEW GP CHRACETERISTICS
foreach var of varlist mean_age mean_male mean_dk solo ku au sdu other N_doctors help3 NewSample {
	cap drop help
	cap drop _`var'
	g help=`var' if share>=0.85 & timing==1
	bys gp_fe: egen _`var'=mode(help)
}

preserve 

** OLD GP CHARACTERISTICS
drop mean_age mean_male mean_dk solo ku au sdu other N_doctors
rename ydernr ydernr_copy
rename gp_fe ydernr
merge m:1 ydernr year using "$work\gp_ses.dta"
drop if _merge==2
drop _merge
rename ydernr gp_fe
rename ydernr_copy ydernr


foreach var of varlist mean_age mean_male mean_dk solo ku au sdu other N_doctors  {
	cap drop help
	cap drop _`var'_old
	g help=`var' if gp_fe==ydernr & timing==-1
	bys gp_fe: egen _`var'_old=mode(help)
}


** PATIENT CHRACTERISTICS
foreach var of varlist male age non_dk married {
	cap drop help
	cap drop _`var'_pt
	g help=`var' if gp_fe==ydernr & timing==-1
	bys gp_fe: egen _`var'_pt=mean(help)
}

	cap drop help
	cap drop _kom_pt
	g help=kom if gp_fe==ydernr & timing==-1
	bys gp_fe: egen _kom_pt=mode(help)



cap drop old_gp_ses _old_gp_ses
g old_gp_ses=yder_ses_max if gp_fe==ydernr & timing==-1
bys pnr: egen _old_gp_ses=mode(old_gp_ses)



********************************************************************************
* Test for selection in who buys which clinic
********************************************************************************


keep if timing==-1
keep if _NewSample==1
keep _help3 _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _other_old ///
_mean_age _mean_male _mean_dk _solo _ku _au _sdu _other  ///
  gp_fe year _male_pt _age_pt _non_dk_pt _married_pt _kom_pt
duplicates drop

replace _ku=0 if _ku==.
replace _au=0 if _au==.
replace _sdu=0 if _sdu==.

drop if _mean_age==.

bys gp_fe: g N=_N
tab N // one obs per closing clinic 
drop N 

*** LABELS
lab var _old_gp_ses "Old physician Low-SES"
lab var _mean_age_old "Old physician Age"
lab var _mean_male_old "Old physician Male"
lab var _mean_dk_old "Old physician Ethnic majority"
lab var _solo_old "Old physician Solo"
lab var _ku_old "Old physician UCHP"
lab var _au_old "Old physician AU"
lab var _sdu_old "Old physician SDU"
lab var _other_old  "Old physician Other uni"

lab var _mean_age "New physician Age"
lab var _mean_male "New physicianMale"
lab var _mean_dk "New physicianEthnic majority"
lab var _solo "New physicianSolo"
lab var _ku "New physicianUCHP"
lab var _au "New physicianAU"
lab var _sdu "New physicianSDU"
lab var _other  "New physicianOther uni"

lab var _male_pt "Patient share male"
lab var _age_pt "Patient average age"
lab var _non_dk_pt "Patient share ethnic majority"
lab var _married_pt "Patient share married"

g _mean_majority_old=(_mean_dk_old>0.5)
lab var _mean_majority_old "Ethnic majority"

ttest _help3, by(_old_gp_ses)
ttest _help3, by(_mean_majority_old)


* TABLE A6
reg _help3 _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old  i.year
test _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old  
local Ftest=`r(F)'
local pval=`r(p)'

outreg2 using "$table\TableA6.doc", replace tex label nocons bdec(5) sdec(5) rdec(5)  adec(5)  addstat(F-test, `Ftest', p-value, `pval') keep(_old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old) ctitle(New PCP Low-SES)



reg _help3 _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _mean_age _mean_male _mean_dk _solo _ku _au _sdu  i.year

test _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _mean_age _mean_male _mean_dk _solo _ku _au _sdu  
local Ftest=`r(F)'
local pval=`r(p)'

outreg2 using "$table\TableA6.doc", append tex label nocons bdec(5) sdec(5) rdec(5)  adec(5)  addstat(F-test, `Ftest', p-value, `pval') keep(_old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _mean_age _mean_male _mean_dk _solo _ku _au _sdu )  ctitle(New PCP Low-SES)



areg _help3 _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _male_pt _age_pt _non_dk_pt _married_pt i.year, a(_kom_pt)
test _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old  _male_pt _age_pt _non_dk_pt _married_pt 

local Ftest=`r(F)'
local pval=`r(p)'


outreg2 using "$table\TableA6.doc", append tex label nocons bdec(5) sdec(5) rdec(5) adec(5) addstat(F-test, `Ftest', p-value, `pval') keep(_old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _male_pt _age_pt _non_dk_pt _married_pt)  ctitle(New PCP Low-SES)




areg _help3 _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _mean_age _mean_male _mean_dk _solo _ku _au _sdu i.year  _male_pt _age_pt _non_dk_pt _married_pt i.year, a(_kom_pt)


test _old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _mean_age _mean_male _mean_dk _solo _ku _au _sdu _male_pt _age_pt _non_dk_pt _married_pt
local Ftest=`r(F)'
local pval=`r(p)'

outreg2 using "$table\TableA6.doc", append tex label nocons bdec(5) sdec(5) rdec(5)  adec(5)  addstat(F-test, `Ftest', p-value, `pval') keep(_old_gp_ses _mean_age_old _mean_male_old _mean_dk_old _solo_old _ku_old _au_old _sdu_old _mean_age _mean_male _mean_dk _solo _ku _au _sdu _male_pt _age_pt _non_dk_pt _married_pt)  ctitle(New PCP Low-SES)



restore 


********************************************************************************
*** DDD
********************************************************************************

cap drop interaction2 interaction3 ddd3
g interaction2=low_ses*_help3
g interaction3=low_ses*post
g ddd3=low_ses*_help3*post


label var _help3 "PCP low SES"
label var post "Post"
label var low_ses "Patient low SES"
label var interaction "PCP low SES x Post"
label var interaction2 "PCP low SES x Patient low SES"
label var interaction3 "Patient low SES x Post"
label var ddd3 "PCP low SES x Patient low SES x Post"


********************************************************************************
********************************************************************************

sum death if _NewSample==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death if _NewSample==1 & post==1 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death if post==1 & _help3==0 &  _NewSample==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)


reghdfe death _help3 low_ses interaction2 $patient_X if post==1 & _NewSample==1, a(gp_fe year) cluster(newgpyr)


outreg2 using "$table\TableA7.doc", replace tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Death)


sum death_CVC if _NewSample==1, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum death_CVC if _NewSample==1 & post==1 & low_ses==1 & male==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest death_CVC if post==1 & _help3==0 &  _NewSample==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe death_CVC _help3 low_ses interaction2 $patient_X if post==1 & _NewSample==1 & male==1, a(gp_fe year) cluster(newgpyr)

outreg2 using "$table\TableA7.doc", append tex label keep(interaction2) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Death from CVC)


sum N_visits if _NewSample==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum N_visits if _NewSample==1 & post==0 & low_ses==1 & male==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest N_visits if post==1 & _help3==0 &  _NewSample==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe N_visits post _help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if _NewSample==1, a(id year) cluster(id)

outreg2 using "$table\TableA7.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Number of visits)


sum statins if _NewSample==1 & male==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum statins if _NewSample==1 & post==0 & low_ses==1 & male==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest statins if post==1 & _help3==0 &  _NewSample==1 & male==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe statins post _help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if _NewSample==1 & male==1, a(id year) cluster(id)

outreg2 using "$table\TableA7.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Statins)


sum diabetes_control if _NewSample==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum diabetes_control if _NewSample==1 & post==0 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest diabetes_control if post==1 & _help3==0 &  _NewSample==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe diabetes_control post _help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if _NewSample==1, a(id year) cluster(id)

outreg2 using "$table\TableA7.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(Diabetes Checkup)


sum ACSC_COPD if _NewSample==1 & post==0, d
local mean=round(`r(mean)'*1000/1000, 0.00001)
local sd=round(`r(sd)'*1000/1000, 0.00001)

sum ACSC_COPD if _NewSample==1 & post==0 & low_ses==1, d
local mean_low=round(`r(mean)'*1000/1000, 0.00001) 

ttest ACSC_COPD if post==1 & _help3==0 &  _NewSample==1, by(low_ses)
local mean0= round((`r(mu_2)'-`r(mu_1)')*1000/1000, 0.00001)

reghdfe ACSC_COPD post _help3 low_ses interaction interaction2 interaction3 ddd3 $patient_X  if _NewSample==1, a(id year) cluster(id) 

outreg2 using "$table\TableA7.doc", append tex label keep(ddd3) nocons bdec(5) sdec(5) rdec(5) addtext(Outcome mean,`mean', Outcome mean low-SES, `mean_low', Gradient  for high SES physicians, `mean0') ctitle(COPD Hospitalization)




